<template>
  <div class="wrapper">
    <div class="center-box">
      <div class="logo">
        <img
          src="https://img1.baidu.com/it/u=1653443696,112616033&fm=26&fmt=auto&gp=0.jpg"
          alt=""
        />
      </div>
      <el-form ref="form" label-width="80px" :rules="rules" :model="formData">
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="formData.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="formData.password"
            prefix-icon="el-icon-s-check"
            type="password"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="button">
        <el-button type="primary" @click="onLogin">登录</el-button>
        <el-button type="info" @click="onReset">重置</el-button>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      formData: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入账户', trigger: 'blur' },
          { min: 3, max: 10, message: '账户在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '密码在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onLogin() {
      this.$refs.form.validate(async (config) => {
        if (!config) {
          return;
        }
        const { data: res } = await this.$http.post('login', this.formData);
        if (res.meta.status !== 200) return this.$message.error('登录失败');
        this.$message.success('登录成功');
        window.sessionStorage.setItem('token', res.data.token);
        this.$router.push('/home');
      });
    },
    onReset() {
      this.$refs.form.resetFields();
    }
  }
}
</script>
<style lang="scss" scoped>
.wrapper {
  height: 100%;
  background: #2B4B6B;
  .center-box {
    width: 450px;
    height: 304px;
    position: relative;
    top: 50%;
    left: 50%;
    box-sizing: border-box;
    padding: 0 50px;
    transform: translate(-50%, -50%);
    background-color: #fff;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 10px #ddd;
    .el-form {
      margin-top: 100px;
    }
    .button {
      text-align: end;
    }
    .logo {
      position: absolute;
      left: 50%;
      width: 100px;
      height: 100px;
      padding: 10px;
      border: 1px solid #efefef;
      border-radius: 50%;
      background: white;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #efefef;
      }
    }
  }
}
</style>
